package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.CityInfo;
import com.lanlian.commonlib.bean.ProvinceInfo;
import com.lanlian.commonlib.bean.ReqLazyEnum;
import com.lanlian.commonlib.bean.RespLazyEnum;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.utillib.ToastUtils;

import java.util.ArrayList;
import java.util.TreeMap;


public class CitySelectorWindow extends Dialog implements View.OnClickListener {

    private TextView mTvCancel;
    private TextView mTvOk;
    private CityPickerLayout mLayCity;

    private String mProvinceId;
    private String mCityId;

    private Context mContext;
    private ArrayList<ProvinceInfo> mProvinceList = new ArrayList<>();

    private OnCityChooseListener mOnCityChooseListener;
    private RespLazyEnum mRespUtilEnum;

    public void setOnCityChooseListener(OnCityChooseListener onCityChooseListener) {
        this.mOnCityChooseListener = onCityChooseListener;
    }

    public interface OnCityChooseListener {
        void onCityChoose(ProvinceInfo provinceInfo, CityInfo cityInfo);
    }

    public CitySelectorWindow(final Context context) {
        super(context, R.style.customDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_window_wheelview);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        initView();
        setListeners();
        getProvince();
        if (!TextUtils.isEmpty(mProvinceId) && !TextUtils.isEmpty(mCityId)) {
            mLayCity.defaultProvince(mProvinceId);
            mLayCity.defaultCity(mCityId);
        }
    }

    private void initView() {
        mTvCancel = findViewById(R.id.tv_cancel);
        mTvOk = findViewById(R.id.tv_ok);
        mLayCity = findViewById(R.id.lay_city);
    }

    private void setListeners() {
        mTvOk.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                if (mLayCity.getProvince() != null && mLayCity.getCity() != null) {
                    mOnCityChooseListener.onCityChoose(mLayCity.getProvince(), mLayCity.getCity());
                    dismiss();
                } else {
                    ToastUtils.show(mContext, "请选择开户省市");
                }
                break;
            default:
                break;
        }
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

    private void getProvince() {
        VolleyRequestUtils.getInstance().getLazyEnum(mContext.toString(),
                true, true, new ReqLazyEnum(), new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mRespUtilEnum = (RespLazyEnum) response;
                        mProvinceList.clear();
                        for (TreeMap.Entry<String, String> entry : mRespUtilEnum.getProvince().entrySet()) {
                            ProvinceInfo provinceInfo = new ProvinceInfo();
                            provinceInfo.setId(entry.getKey());
                            provinceInfo.setName(entry.getValue());
                            mProvinceList.add(provinceInfo);
                        }
                        mLayCity.setAreaInfo(mProvinceList, mRespUtilEnum);
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {

                    }
                });
    }

    public void setCityId(String provinceId, String cityId) {
        this.mProvinceId = provinceId;
        this.mCityId = cityId;
    }

}
